package com.xxwy.ddu.security.code.config;

import com.xxwy.ddu.security.code.properties.SecurityConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

/**
 * @author xxwy
 * on 2018/8/24 0024
 * 密码登录配置
 */
public class PasswordAuthenticationConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    AuthenticationFailureHandler authenticationFailureHandler;

    /**
     * 认证成功Handler
     * @return
     */
    @Autowired
    AuthenticationSuccessHandler authenticationSuccessHandler;

    protected void applyPasswordAuthenticationConfig(HttpSecurity http) throws Exception {
        http.formLogin()
                .usernameParameter("user").passwordParameter("pwd")
                .loginPage(SecurityConstants.DEFAULT_UNAUTHENTICATION_URL)                      // 登录页面回调
                .loginProcessingUrl(SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_FORM)        // 自定义的登录接口
                .successHandler(authenticationSuccessHandler)                                 // 认证成功回调
                .failureHandler(authenticationFailureHandler);                                // 认证失败回调
    }


}
